//
//  WmcCustomVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/20.
//

import UIKit

open class WmcCustomVC: WmcBaseVC {
    // Lifecycle

    // MARK: 实例化

    public convenience init(customView: WmcCustomViewProtocol) {
        self.init(nibName: nil, bundle: nil)
        self.customView = customView
    }

    override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    @available(*, unavailable)
    public required convenience init?(coder: NSCoder) {
        self.init(nibName: nil, bundle: nil)
    }

    // MARK: 生命周期回调

    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        customView?.viewWillAppear(.current, animated: animated)
    }

    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        customView?.viewDidAppear(.current, animated: animated)
    }

    override open func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        customView?.viewWillDisappear(.current, animated: animated)
    }

    override open func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        customView?.viewDidDisappear(.current, animated: animated)
    }

    override open func viewDidLoad() {
        super.viewDidLoad()
        customView?.viewDidLoad(.current)
        // 驱动注册
        registerDriving()
        // 进行布局UI
        customView?.makeUI(.current)
        // 绑定视图事件
        customView?.bindViewAction(.current)
        // 绑定视图模型
        customView?.bindViewModel(.current)
    }

    // Internal

    /// 自定义视图
    var customView: WmcCustomViewProtocol?

    /// 注册驱动
    func registerDriving() {
        // 标题驱动
        YPWmcDrivingManager.shared.custom_setTitleBlock = { [weak self] title in
            guard let weakSelf = self else { return }
            weakSelf.title = title
        }

        // 返回驱动
        YPWmcDrivingManager.shared.custom_backViewControllerBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.backAction()
        }

        // VC推出驱动
        YPWmcDrivingManager.shared.custom_pushCustomViewBlock = { view in
            return WmcNavigatorManager.pushCustom(view)
        }

        // VC推出路由
        YPWmcDrivingManager.shared.custom_pushNavigationBlock = { path, style, vm, animated, completion in
            return WmcNavigatorManager.push(path, style: style, parms: vm, animated: animated, completion: completion)
        }
    }

    /// 点击返回
    override func backAction() {
        guard let customView = customView else {
            super.backAction()
            return
        }
        let isBack = customView.backAction(.current)
        if isBack { // 允许返回
            let style = customView.popTransitionStyle(.current)
            let animated = customView.popTransitionAnimated(.current)
            super.popVC(style: style, animated: animated)
        }
    }
    // MARK: 布局

    override func makeUI() {
        guard let customView = customView else { return }
        // 添加视图
        addSubview(customView)
        // 是否使用导航
        let isUseNavigationBar = customView.navigationBarUse(.current)
        if isUseNavigationBar { // 使用
            let style = customView.navigationBarStyle(.current)
            let showShadow = customView.navigationBarUseShadow(.current)
            makeNavView(style: style, showShadow: showShadow)

            customView.snp.remakeConstraints { make in
                make.top.equalTo(navView.snp.bottom)
                make.left.right.bottom.equalToSuperview()
            }
        } else { // 不使用
            customView.snp.remakeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
    }
}
